const fs = require('fs');
class ExecuteChain {
    constructor() {

    }
    setData(key, value) {
        this[key] = value
    }
}
class Files {
    constructor() {
        this.chain = new ExecuteChain()
        for (let i in fs) {
            if (typeof fs[i] === 'function') {
                this[i] = this.BRIDGING(i)
            }
        }
    }
    // 函数桥接
    BRIDGING(key) {
        return ({ path, callback = (e, o) => e, success }) => {
            return fs[key](path, (err, data) => {
                if (err) {
                    throw new Error(`${key}:${err}`);
                }
                // this.chain.setData(key, callback(data, this.chain))
                if (success) {
                    success(err, data)
                }
            })
        }
    }
    execute(arr) {
        let [unit, ...params] = arr
        if (!unit) return
        this[unit.name]({
            path: unit.path,
            callback: unit.callback,
            mounted: () => {
                this.execute(params)
            }
        })
    }
}

module.exports = Files